package com.yss.reportworld.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.exception.base.BaseException;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.reportworld.service.*;
import com.yss.reportworld.util.TableInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.reportworld.domain.Datasql;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 函数信息Controller
 *
 * @author yss
 * @date 2023-03-27
 */
@RestController
@RequestMapping("/datasql")
public class DatasqlController extends BaseController {
    @Autowired
    private IDatasqlService datasqlService;
    @Autowired
    private IModularService modularService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private ISqlScriptService sqlScriptService;

    /**
     * 查询函数信息列表
     */
    @RequiresPermissions("reportworld:datasql:list")
    @GetMapping("/list")
    public TableDataInfo list(Datasql datasql) {
        startPage();
        List<Datasql> list = datasqlService.selectDatasqlList(datasql);
        return getDataTable(list);
    }

    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:datasql:list")
    @Log(title = "函数信息", businessType = BusinessType.OTHER)
    @PostMapping("/init")
    public AjaxResult init(@RequestBody Datasql datasql) {
        try {
            TableInfoUtil.handleTableVersion(datasql.getHisVersion()); // 替换表名
            AjaxResult ajax = AjaxResult.success();
            //加载所属模块，客户，报表
            ajax.put("modularTree", modularService.selectModularTreeList());
            ajax.put("customerTree", customerService.selectCustomerTreeList());
            ajax.put("reportTree", reportService.selectReportTreeList());
            ajax.put("hisVersions", sqlScriptService.getReductionVersion(false));
            return ajax;
        } catch (Exception ex) {
            throw new BusinessException("初始化加载数据出错，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    /**
     * 导出函数信息列表
     */
    @RequiresPermissions("reportworld:datasql:export")
    @Log(title = "函数信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Datasql datasql) {
        List<Datasql> list = datasqlService.selectDatasqlList(datasql);
        EasyExeclUtil.export(response, list, Datasql.class);
    }

    /**
     * 获取函数信息详细信息
     */
    @RequiresPermissions("reportworld:datasql:query")
    @PostMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestBody Datasql datasql) {
        return success(datasqlService.selectDatasqlById(datasql));
    }

    /**
     * 新增函数信息
     */
    @RequiresPermissions("reportworld:datasql:add")
    @Log(title = "函数信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Datasql datasql) {
        if (!datasqlService.checkDatasqlNameUnique(datasql)) {
            return error("SQL脚本名称【" + datasql.getName() + "】，数据库类型【" + datasql.getDataBaseType() + "】已存在");
        }
        AjaxResult ajax = AjaxResult.success();
        datasqlService.addDatasql(datasql);
        return ajax;

    }

    /**
     * 修改函数信息
     */
    @RequiresPermissions("reportworld:datasql:edit")
    @Log(title = "函数信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Datasql datasql) {
        if (!datasqlService.checkDatasqlNameUnique(datasql)) {
            return error("SQL脚本名称【" + datasql.getName() + "】，数据库类型【" + datasql.getDataBaseType() + "】已存在");
        }
        AjaxResult ajax = AjaxResult.success();
        datasqlService.editDatasql(datasql);
        return ajax;
    }

    /**
     * 删除函数信息
     */
    @RequiresPermissions("reportworld:datasql:remove")
    @Log(title = "函数信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody Datasql datasql) {
        datasqlService.deleteDatasql(datasql);
        return success();
    }
}
